package com.geek.novel.network

import com.geek.novel.common.Constant
import com.geek.novel.repository.HttpRepository
import com.geek.novel.repository.WebRepository
import com.geek.novel.utils.SPUtil

/**
 *
 * Created by  on 2017/8/8.
 */
object RepositoryProvider {

    private var repository: HttpRepository? = null

    fun provideRepository(): HttpRepository {
        WebRepository.token = SPUtil[Constant.SP_TOKEN, "", SPUtil.usrInfo] as String?
        if(repository == null){
            repository = HttpRepository(WebRepository.create())
        }
        return repository!!
    }


//    private var webService: WebService? = null

    //请求超时时间
//    private val DEFAULT_TIMEOUT: Long = 10000

    /*private fun getDefault(): WebService {
        val token: String? = null
        val client: OkHttpClient.Builder = OkHttpClient.Builder()
        client.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);

//        private fun getHttpLoggingInterceptor(): HttpLoggingInterceptor {
//            val loggingInterceptor = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { message ->
//                LogCatUtil.e("", "==============getHttpLoggingInterceptor==" + message)
//            })
//            loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
//            return loggingInterceptor;
//        }

//        if (webService == null) {
            //手动创建一个OkHttpClient并设置超时时间
            client.addInterceptor(Interceptor { chain ->
                val request = chain.request()
//                if (!CommUtil.isNull(token)) {
                    val requestBuilder = request?.newBuilder()?.addHeader("clientkey", SPUtil[MyConstant.SP_TOKEN, token!!, SPUtil.usrInfo] as String)?.addHeader("Cache-Control", "no-cache")
                    val newRequest = requestBuilder?.build()
                    return@Interceptor chain.proceed(newRequest)
//                }
//                chain.proceed(request)
            })
            val retrofit = Retrofit.Builder()
                    .client(client.build())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .baseUrl(MyConstant.Http_SERVICE_IP)
                    .build()
//            webService=
            return retrofit.create(WebService::class.java)
//        }

//        return webService as WebService
    }
*/
//    var token: String? = null
//    var client: OkHttpClient.Builder = OkHttpClient.Builder()

//    private fun getHttpLoggingInterceptor(): HttpLoggingInterceptor {
//        val loggingInterceptor = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { message ->
//            Log.e("", "==============msg==" + message)
//        })
//        loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
//        return loggingInterceptor;
//    }

    /*fun create(): WebService {
        val retrofit = Retrofit.Builder()
                .client(client.build())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .baseUrl(MyConstant.Http_SERVICE_IP)
                .build()
        client.addInterceptor(Interceptor { chain ->
            val request = chain.request()
//            if (!CommUtil.isNull(token)) {

           var requestBuilder=request?.newBuilder()

            if (requestBuilder!=null){
                requestBuilder =requestBuilder.addHeader("clientkey", SPUtil[MyConstant.SP_TOKEN, token!!, SPUtil.usrInfo] as String)

                val newRequest = requestBuilder.build()
                return@Interceptor chain.proceed(newRequest)
            }
//            }
            chain.proceed(request)
        })
        return retrofit.create(WebService::class.java)
    }*/

}